package com.woniu.market_stall.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Or;
import com.woniu.market_stall.entity.Log;
import com.woniu.market_stall.entity.Order;
import com.woniu.market_stall.mapper.LogMapper;
import com.woniu.market_stall.mapper.OrderMapper;
import com.woniu.market_stall.mapper.StallMapper;
import com.woniu.market_stall.vo.RentOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class RentOrderService {

    @Autowired
    OrderMapper rentOrderMapper;
    @Autowired
    StallMapper stallMapper;
    @Autowired
    LogMapper logMapper;

    public PageInfo<RentOrderVo> queryByPage(Integer now, Integer size, String orderId, Integer statusPay) {
        PageHelper.startPage(now,size);

        List<RentOrderVo> rentOrderVoList = rentOrderMapper.selectRentOrderByOrderIdAndStatus(orderId,statusPay);

        return new PageInfo<>(rentOrderVoList);
    }

    public Order queryRentOrderByOrderId(Long orderId) {
        return rentOrderMapper.selectRentOrderByOrderId(orderId);
    }

    public void removeOrderById(String orderId) {
        rentOrderMapper.deleteOrderById(orderId);
    }

    /**
     * 修改租赁订单的同时，要将租赁的摊位的状态改为使用中，租户id修改，且类型为租赁类型
     * @param order
     */
    @Transactional
    public void modifyRentOrderByOrderId(Order order) {
        stallMapper.updateRentStallStatusAndTypeByStallId(order.getStallId(),order.getUserId());
        rentOrderMapper.updateRentOrderByOrderId(order);
        Log log=new Log();
        log.setManagerId(order.getId());
        log.setContent("修改了租赁订单");
        log.setInsertTime(new Date());
        log.setIsDelete(0);
        logMapper.insert(log);
    }

    /**
     * 插入租赁订单的同时，要将租赁的摊位的状态改为使用中，租户id修改，且类型为租赁类型
     * @param order
     */
    @Transactional
    public void add(Order order) {
        stallMapper.updateRentStallStatusAndTypeByStallId(order.getStallId(),order.getUserId());
        rentOrderMapper.insertRentOrder(order);
        Log log=new Log();
        log.setManagerId(order.getId());
        log.setContent("新增了租赁订单");
        log.setInsertTime(new Date());
        log.setIsDelete(0);
        logMapper.insert(log);
    }

    public PageInfo<Order> queryContractByPage(Integer now, Integer size, String contract) {
        PageHelper.startPage(now,size);

        List<Order> contractList = rentOrderMapper.selectContractByPage(contract);

        return new PageInfo<>(contractList);
    }

    public List<Order> queryAllPayment() {
        return rentOrderMapper.selectAll();
    }
}
